﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QT.Windows.Shell;

namespace IISLogAnalytics
{
    public partial class IISLogAnalytics : GlassForm
    {
        #region IISLogAnalytics

        public IISLogAnalytics()
        {
            InitializeComponent();
        }

        private void IISLogAnalytics_Load(object sender, EventArgs e)
        {
            DataTable dt = DBO.getSiteList();
            this.cbbSiteName.DisplayMember = "domain";
            this.cbbSiteName.ValueMember = "sitename";
            this.cbbSiteName.DataSource = dt;

            this.dtpStart.Value = DateTime.Now.Subtract(new TimeSpan(1, 0, 0, 0));
        }

        private void IISLogAnalytics_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            if (MessageBox.Show("确认要关闭程序吗？", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel) return;
            e.Cancel = false;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            this.menuStrip1.BackColor = AeroColor;
            this.statusStrip1.BackColor = AeroColor;
        }

        #endregion

        #region Event

        #region Button

        private void gbtnAnaly_Click(object sender, EventArgs e)
        {
            toolStripMenuItem_Analy_Click(sender, e);
        }

        private void gbtnViewbyIP_Click(object sender, EventArgs e)
        {
            if (this.cbbSiteName.SelectedValue == null)
            {
                MessageBox.Show("请选择站点");
                return;
            }

            if (!(this.cbTel.Checked || this.cbUni.Checked))
            {
                MessageBox.Show("请选择线路类型");
                return;
            }

            #region Init

            string sitename = this.cbbSiteName.SelectedValue.ToString();
            List<string> stype = new List<string>();
            if (this.cbTel.Checked) stype.Add("1");
            if (this.cbUni.Checked) stype.Add("0");

            #endregion

            DataTable dt = DBO.getAnalybyIP(this.dtpStart.Value.ToString("yyyy-MM-dd"), this.dtpEnd.Value.ToString("yyyy-MM-dd"), sitename, stype, (int)this.nudTop.Value);
            this.dgvResult.DataSource = dt;
            this.dgvResult.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
        }

        private void gbtnViewbyPage_Click(object sender, EventArgs e)
        {
            if (this.cbbSiteName.SelectedValue == null)
            {
                MessageBox.Show("请选择站点");
                return;
            }

            if (!(this.cbTel.Checked || this.cbUni.Checked))
            {
                MessageBox.Show("请选择线路类型");
                return;
            }

            #region Init

            string sitename = this.cbbSiteName.SelectedValue.ToString();
            List<string> stype = new List<string>();
            if (this.cbTel.Checked) stype.Add("1");
            if (this.cbUni.Checked) stype.Add("0");

            #endregion

            DataTable dt = DBO.getAnalybyPage(this.dtpStart.Value.ToString("yyyy-MM-dd"), this.dtpEnd.Value.ToString("yyyy-MM-dd"), sitename, stype, (int)this.nudTop.Value);
            this.dgvResult.DataSource = dt;
            this.dgvResult.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
        }

        private void gbtnViewbyGeneral_Click(object sender, EventArgs e)
        {
            if (this.cbbSiteName.SelectedValue == null)
            {
                MessageBox.Show("请选择站点");
                return;
            }

            #region Init

            string sitename = this.cbbSiteName.SelectedValue.ToString();

            #endregion

            DataTable dt = DBO.getAnalybyGeneral(this.dtpStart.Value.ToString("yyyy-MM-dd"), this.dtpEnd.Value.ToString("yyyy-MM-dd"), sitename);
            this.dgvResult.DataSource = dt;
            this.dgvResult.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
        }

        #endregion

        #region MenuStrip

        #region File

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion

        #region Edit

        private void toolStripMenuItem_Analy_Click(object sender, EventArgs e)
        {
            Analy myImport = new Analy();
            myImport.ShowDialog();
            myImport.Dispose();
        }

        private void toolStripMenuItem_PageComment_Click(object sender, EventArgs e)
        {
            PageComment myComment = new PageComment();
            myComment.ShowDialog();
            myComment.Dispose();
        }

        private void toolStripMenuItem_ClearData_Click(object sender, EventArgs e)
        {
            DataManager myData = new DataManager();
            myData.ShowDialog();
            myData.Dispose();
        }

        #endregion

        #endregion

        #region DataGridView

        private void dgvResult_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            this.tsslMsg.Text = string.Format("共{0}条记录", this.dgvResult.RowCount);
        }

        private void dgvResult_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1) return;
            this.tsslPosition.Text = string.Format("      行 {0}      列 {1}", e.RowIndex + 1, e.ColumnIndex + 1);
        }

        #endregion

        #region ContextMenuStrip

        private void toolStripMenuItem_ExportAll_Click(object sender, EventArgs e)
        {
            myExport(true);
        }

        private void toolStripMenuItem_ExportSelected_Click(object sender, EventArgs e)
        {
            if (this.dgvResult.SelectedRows.Count == 0)
            {
                MessageBox.Show("请选择要导出的记录");
                return;
            }

            myExport(false);
        }

        #endregion

        #endregion

        #region Method

        private void myExport(bool b)
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.ValidateNames = true;
            saveFileDialog1.Filter = "日志文件(*.log)|*.log|CSV文件(*.csv)|*.csv";
            saveFileDialog1.FilterIndex = 1;
            saveFileDialog1.DefaultExt = "log";
            saveFileDialog1.FileName = "";
            saveFileDialog1.AddExtension = true;
            if (saveFileDialog1.ShowDialog() == DialogResult.Cancel)
            {
                return;
            }

            if (saveFileDialog1.FileName.EndsWith(".csv"))
            {
                if (Export.Export2CSV(this.dgvResult, saveFileDialog1.FileName, b))
                {
                    MessageBox.Show("导出成功");
                }
                else
                {
                    MessageBox.Show("操作失败");
                    return;
                }
            }
            else
            {
                if (Export.Export2Txt(this.dgvResult, saveFileDialog1.FileName, b))
                {
                    MessageBox.Show("导出成功");
                }
                else
                {
                    MessageBox.Show("操作失败");
                    return;
                }
            }
        }

        #endregion
    }
}
